<?
define("NO_KEEP_STATISTIC", true);
define("NO_LANG_FILES", true);
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");

if(CModule::IncludeModule("compression"))
	CCompress::Disable2048Spaces();

global $DB;

if (!CModule::IncludeModule("consult"))
	return;

$stream_id = intval ($_REQUEST["stream_id"]);
$last_id = intval ($_REQUEST["last_id"]);
$r = intval ($_REQUEST["r"]);

$curUserID = intval ($_REQUEST["u"]);
if ($curUserID <= 0)
	$curUserID = CConsultUser::GetCurConsultUser();

if ($stream_id > 0 && check_bitrix_sessid())
{
	//cur user is online in stream
	CConsultUserOnline::SetUserOnline($stream_id, $curUserID);
	//end

	$dbRes = CConsultUserOnline::GetUsersOnline($stream_id);

	while($arOnline = $dbRes->Fetch())
	{
		$strAlt = "title=\"".$arOnline["NAME"]."\"";
		$strPhoto = CConsultUser::GetUserPhotoHtml($arOnline["PHOTO"], array("X"=>50, "Y"=>50), "/bitrix/images/consult/nopic_30x30.gif", $strAlt);
		if ($strPhoto)
			$arOnline["PHOTO"] = $strPhoto;
		$arResult["ONLINE"][] = $arOnline;
	}
	
	$obConsMess = new COnline_message;
	$arMess = $obConsMess->GetMessagesForChat($stream_id, $last_id);
	if ($arMess)
	{
		foreach ($arMess as $mess)
		{
			if (CConsultUser::IsAdmin($mess["REAL_USER_ID"]) || CConsultUser::IsConsultTeam($mess["REAL_USER_ID"]))
			{
				$mess["IS_SUPPORT"] = "Y";
			}
			$mess["MESSAGE"] = htmlspecialchars($mess["MESSAGE"]);
			$arResult["MESSAGES"][] = $mess;
		}
	}

	$obStream = new CConsultStream;
	$arStream = $obStream->GetByID($stream_id);

	if ($arStream && $r != $arStream["RESPONSIBLE_ID"])
	{
		$consUser = new CConsultUser;
		$arResp = $consUser->GetByID($arStream["RESPONSIBLE_ID"]);	
		if ($arResp)
		{
			$arResult["RESPONSIBLE"] = $arResp;
			$strPhoto = CConsultUser::GetUserPhotoHtml($arResp["PHOTO"], array("X"=>50, "Y"=>50));
			
			if ($strPhoto)
				$arResult["RESPONSIBLE"]["PHOTO"] = $strPhoto;
		}
	}
	echo CUtil::PhpToJSObject($arResult);
}
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_after.php");
?>